package com.niiwoo.civet.trade.dao.mapper;

import com.niiwoo.civet.trade.dao.entity.FanLiTouProjectInvestOrder;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestOrder;
import com.niiwoo.civet.trade.dto.request.InvestOrderListRequestDTO;
import com.niiwoo.civet.trade.dto.request.InvestOrderStatusRequestDTO;
import com.niiwoo.civet.trade.dto.request.fanlitou.FanLiTouRequestDTO;
import com.niiwoo.civet.trade.dto.response.DripSummaryInfoRespDTO;
import com.niiwoo.civet.trade.dto.response.InvestOrderMaxandMinDateResponseDTO;
import com.niiwoo.civet.trade.report.dto.ProjectInvestOrderExt;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

public interface ProjectInvestOrderMapperExt extends ProjectInvestOrderMapper{

    BigDecimal selectInvestedAmount(@Param("projectId") Long projectId);

    BigDecimal selectInvestingAmount(@Param("projectId") Long projectId);

    /**
     * 通过projectId查询标的投资记录列表
	 * @param projectId
     * @return
     */
    List<ProjectInvestOrder> queryProjectInvestOrderListOrderByCreateTimeAscByProjectId(@Param("projectId") Long projectId);

	ProjectInvestOrder selectByPrimaryKeyForUpdate(@Param("investOrderId") Long investOrderId);

	List<ProjectInvestOrder> selectDealingList();

	List<ProjectInvestOrder> selectMyInvestOrderList(@Param("userId") String userId, @Param("orderStatus") Integer orderStatus,
	                                                 @Param("offset") int offset, @Param("pageSize") Integer pageSize);

	List<ProjectInvestOrder> selectInvestedList(@Param("projectId") Long projectId);

	/**
	 * 获取投资订单列表（用于生成合同）
	 * @param projectId
	 * @return
	 */
	List<ProjectInvestOrder> selectInvestSuccessList(@Param("projectId") Long projectId);

	Integer countInvested(@Param("projectId") Long projectId);

	List<ProjectInvestOrder> selectPageInvestedList(@Param("projectId") Long projectId, @Param("offset") int offset, @Param("pageSize") int pageSize);


	/**
	 * 查询用户投资该标的且支付成功的金额
	 * @param userId
	 * @return
	 */
	List<ProjectInvestOrder> selectInvestedByProjectIdAndUserId(@Param("userId") String userId, @Param("projectId") Long projectId);

	List<ProjectInvestOrder> selectInvestOrderListByProjectIdAndUserId(@Param("userId") String userId, @Param("projectId") Long projectId);

	int selectInvestingCountByUserId(@Param("userId") String userId);

	int selectSuccessCountByUserId(@Param("userId") String userId);

	/**
	 * 查询最近一笔投资成功订单
	 * @param userId
	 * @return
     */
	ProjectInvestOrder selectLastOneSuccessByUserId(String userId);

	/**
	 * 查询用户累计投资成功金额
	 * @param userId
	 * @return
     */
	BigDecimal selectTotalInvestSuccessAmountByUserId(String userId);

	/**
	 * 查询用户投资中但未满标的总金额
	 * @param userId
	 * @return
	 */
	BigDecimal selectInvestingAndNotFullAmountByUserId(String userId);

	int cancelDealingOrder(ProjectInvestOrder investOrder);

	List<ProjectInvestOrder> getMyInvestOrderList(InvestOrderStatusRequestDTO requestDTO);

	Integer getMyInvestOrderCount(InvestOrderStatusRequestDTO requestDTO);

	/**
	 * 通过creditorSaleDetailId查询出让的债权投资记录列表
	 * @param creditorSaleDetailId
	 * @return
	 */
	ProjectInvestOrder queryInvestSuccessfulByCreditorSaleDetailId(@Param("creditorSaleDetailId") Long creditorSaleDetailId);

	List<ProjectInvestOrder> selectByCreditorSaleDetailIdAndUserId(@Param("creditorSaleDetailId") Long creditorSaleDetailId, @Param("userId") String userId);

	InvestOrderMaxandMinDateResponseDTO getMyInvestOrderMaxandMinDate(@Param("userId") String userId, @Param("type") Byte type, @Param("queryType") Byte queryType);

	Integer getMyInvestCountByProdrctType(InvestOrderStatusRequestDTO requestDTO);


	/**
	 * 获取某时间段返利投用户的投资记录总数
	 * @param userList
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	int selectTotalCountByUserList(@Param("userList") List<FanLiTouRequestDTO.UserRelation> userList, @Param("startTime") String startTime, @Param("endTime") String endTime);

	/**
	 * 获取某时间段返利投用户的提前还款记录总数
	 * @param userList
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	int selectRepayTotalCountByUserList(@Param("userList") List<FanLiTouRequestDTO.UserRelation> userList, @Param("startTime") String startTime, @Param("endTime") String endTime);

	/**
	 * 获取某时间段返利投用户的投资记录
	 * @param userList
	 * @param startTime
	 * @param endTime
	 * @param offset
	 * @param pageSize
	 * @return
	 */
	List<ProjectInvestOrder> selectByDetailByUserList(@Param("userList") List<FanLiTouRequestDTO.UserRelation> userList, @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("offset") int offset, @Param("pageSize") int pageSize);

	/**
	 * 获取某时间段返利投用户的提前结清记录
	 * @param userList
	 * @param startTime
	 * @param endTime
	 * @param offset
	 * @param pageSize
	 * @return
	 */
	List<FanLiTouProjectInvestOrder> selectRepayRecordByUserList(@Param("userList") List<FanLiTouRequestDTO.UserRelation> userList, @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("offset") int offset, @Param("pageSize") int pageSize);

	/**
	 * 获取返利投用户的首次投资记录
	 * @param userList
	 * @return
	 */
	List<ProjectInvestOrder> selectFirstInvestByUserList(@Param("userList") List<FanLiTouRequestDTO.UserRelation> userList);

	List<ProjectInvestOrder> getInvestOrderListByUserId(@Param("userId") String userId);

	List<ProjectInvestOrder> selectByInvestOrderIds(@Param("investOrderIdList") Set<Long> investOrderIds);

	List<ProjectInvestOrder> selectFullInvestedListByTime(@Param("startTime")Date startTime,@Param("endTime")Date endTime, @Param("offset") int offset, @Param("pageSize") int pageSize);

	Integer countFullInvestedListByTime(@Param("startTime")Date startTime,@Param("endTime")Date endTime);

	List<ProjectInvestOrder> selectInvestingListByTime(@Param("startTime")Date startTime,@Param("endTime")Date endTime, @Param("offset") int offset, @Param("pageSize") int pageSize);

	Integer countInvestingListByTime(@Param("startTime")Date startTime,@Param("endTime")Date endTime);

	/**
	 * PC查询原始投资订单列表(不考虑债转)
	 * @param requestDTO 请求VO
	 */
	List<ProjectInvestOrder> selectOriginalInvestOrderList(InvestOrderListRequestDTO requestDTO);

	/**
	 * PC根据状态查询原始投资订单列表总数
	 */
	Integer selectOriginalInvestOrderCount(InvestOrderListRequestDTO requestDTO);

	/**
	 * 查询用户新手投 已经投资的金额
	 *
	 * @param userId
	 * @param channelCode
	 * @return
	 */
	BigDecimal selectIsNoviceMoneyTotal(@Param("userId") String userId,@Param("channelCode")String channelCode);
	/**
	 * 查询用户活动期间投资订单
	 *
	 * @param userId
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	List<ProjectInvestOrder> selectInvestOrderListForActivty(@Param("userId") String userId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

	ProjectInvestOrder queryFirstInvestInfo(@Param("userId") String userId);

	DripSummaryInfoRespDTO queryMaxInvestForMonth(@Param("userId") String userId);

	DripSummaryInfoRespDTO queryMaxInvestForDay(@Param("userId") String userId);

	DripSummaryInfoRespDTO queryMaxFullProjectForMonth(@Param("userId") String userId);

	DripSummaryInfoRespDTO queryMaxPressureProjectForMonth(@Param("userId") String userId);

	DripSummaryInfoRespDTO queryInvestBorrowerCount(@Param("userId") String userId);
	/**
	 * 通过标的ID查询投资过该标的数据
	 * @param projectId
	 * @return
	 */
	List<Long> findIdsByProjectId(Long projectId);

	int countClosedOrderNumByIds(List<Long> investOrderIds);

	Integer countTransferNumByTime(@Param("startTime")Date startTime,@Param("endTime")Date endTime);

	List<ProjectInvestOrderExt> selectTransferListByTime(@Param("startTime")Date startTime, @Param("endTime")Date endTime, @Param("indexStart") Integer indexStart, @Param("size")Integer size);

	List<Long> selectTransferProjectIdListByTime(@Param("startTime")Date startTime, @Param("endTime")Date endTime);

	ProjectInvestOrder selectFirstInvestByUser(String userId);
	
	/**
	 * 根据用户ID列表查询有投资的用户列表
	 * 
	 * @param userIdList
	 * @return
	 */
	List<String> getHasInvestmentForUserIdList(@Param("userIdList") List<String> userIdList);
}